<?php

namespace App\Traits;

use Carbon\Carbon;

trait HelpFunctions
{
    public function uSN(string $flag, $date)
    {
        $id_main = date('YmdHis', strtotime($date)) . rand(10000000, 99999999);
        //订单号码主体长度
        $id_len = strlen($id_main);
        $id_sum = 0;

        for ($i = 0; $i < $id_len; $i++) {
            $id_sum += (int)(substr($id_main, $i, 1));
        }

        //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
        $sn = $flag . $id_main . str_pad((100 - $id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);

        return $sn;
    }

    public function clientError(string $msg, int $status = 422)
    {
        abort($status, $msg);
    }

    /**
     * 获取数组中指定键的键值对
     */
    public function getArrayByKeys(array $origin, array $keys)
    {
        $result = [];
        foreach ($keys as $key) {
            if (!empty($origin[$key]))
                $result[$key] = $origin[$key];
        }

        return $result;
    }

    public function everyMergeArray(array $origin, array $new)
    {
        foreach ($origin as &$item) {
            $item = array_merge($item, $new);
        }

        return $origin;
    }

    /**
     * 将types='1,3,4'转为
     * [
     *   ['type_id'=>1],
     *   ['type_id'=>3],
     *   ['type_id'=>4],
     * ]
     */
    public function addTypeId(string $types)
    {
        $result = explode(',', $types);
        $types = [];
        foreach ($result as $item) {
            array_push($types, ['type_id' => $item]);
        }

        return $types;
    }

    public function getResourceObjectArgs(array $params, array $new = [])
    {
        $keys = ['link', 'object_id', 'type', 'sort'];
        return $this->filterTwoArrByKeys($keys, $params, $new);
    }

    public function getTypeObjectArgs(string $types, array $new = [])
    {
        $args = $this->addTypeId($types);
        $keys = ['object_id', 'type_id', 'type'];

        return $this->filterTwoArrByKeys($keys, $args, $new);
    }


    public function getOrderArgs(array $params, array $new = [])
    {
        $keys = [
            'status',
            'type',
            'object_id',
            'user_id',
            'sn',
            'created_at',
        ];
        return $this->filterOneArrByKeys($keys, $params, $new);
    }

    public function getServiceArgs(array $params, array $new = [])
    {
        $keys = [
            'title',
            'province_id',
            'city_id',
            'area_id',
            'price',
            'introduction',
            'negotiate',
            'deadline',
            'user_id',
            'sn',
            'created_at',
        ];
        return $this->filterOneArrByKeys($keys, $params, $new);
    }

    public function getDemandArgs(array $params, array $new = [])
    {
        $keys = [
            'title',
            'province_id',
            'city_id',
            'area_id',
            'budget',
            'introduction',
            'negotiate',
            'deadline',
            'user_id',
            'sn',
            'created_at',
        ];
        return $this->filterOneArrByKeys($keys, $params, $new);
    }

    public function getBusinessArgs(array $params, array $new = [])
    {
        $keys = [
            'corporation_name',
            'corporation_card_id',
            'name',
            'landline',
            'province_id',
            'city_id',
            'area_id',
            'serve_begin_time',
            'serve_end_time',
            'introduction',
            'status',
            'readed',
            'user_id',
        ];
        return $this->filterOneArrByKeys($keys, $params, $new);
    }
    private function filterOneArrByKeys(array $keys, array $origin, array $new = [])
    {
        $result = [];
        foreach ($origin as $k => $param) {
            if (in_array($k, $keys)) $result[$k] = $param;
        }

        return array_merge($result, $new);
    }
    private function filterTwoArrByKeys(array $keys, array $origin, array $new)
    {
        $result = [];
        foreach ($origin as $k => $param) {
            foreach ($param as $key => $item) {
                if (in_array($key, $keys)) $result[$k][$key] = $item;
            }
        }

        return $this->everyMergeArray($result, $new);
    }

    /**
     * 判断第一个日期是否比第二个大
     */
    public function compareTime($first, $second)
    {
        $first = Carbon::parse($first);
        $second = Carbon::parse($second);

        return $first->gt($second);
    }
}
